<?php
class ReportBasis 
{
	private $db;
	
	function __construct() {
		$this->db = godhouse::app()->db;
	}
	
	public function find($condition) {
		if($condition) 
			$condition = "WHERE $condition";
		return $this->db->fetch_first("SELECT * FROM god_report_basis $condition");
	}
	
	public function findByPk($pk) {
		return $this->db->fetch_first("SELECT * FROM god_report_basis WHERE reportId='$pk'");
	}
	
	public function findAll($condition='', $page=0, $ppp=GODHOUSE_PPP3, $order='') {
		if($condition) 
			$condition = "WHERE $condition";
		if($order) {
			$condition .= " ORDER BY $order";
		} else 
			$condition .= " ORDER BY reportId DESC";
		
		$sqladd = '';
		if($page && $ppp) {
			$start = ($page-1)*$ppp;
			$sqladd = "LIMIT $start, $ppp";
		}
		return $this->db->fetch_all("SELECT * FROM god_report_basis $condition $sqladd");
	}
	
	public function findAllByPk($pk) {
		return $this->db->fetch_all("SELECT * FROM god_report_basis WHERE reportId='$pk'");
	}
	
	public function update($condition, $att=array()){
		return $this->db->update('god_report_basis', $att, $condition);
	}
	
	public function updateByPk($pk, $att=array()) {
		$condition = "reportId='$pk'";
		return $this->db->update('god_report_basis', $att, $condition);
	}
	
	public function insert($att=array()){
		return $this->db->insert('god_report_basis', $att);
	}
	
	public function delete($condition) {
		$this->db->query("DELETE FROM god_report_basis WHERE $condition");
	}
	
	public function deleteByPk($pk) {
		$condition = "reportId='$pk'";
		$this->db->query("DELETE FROM god_report_basis WHERE $condition");
	}
	
	public function count($condition) {
		if($condition) 
			$condition = "WHERE $condition";
		return $this->db->result_first("SELECT count(*) FROM god_report_basis $condition");
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $order
	 */
	public function inserReportByOrder($order = array('orderId'=>0, 'userId'=>0, 'totalPrice'=>0, 'consigneeId'=>0, 'createdTime'=>0)) {
		// 插入报表数据
		$consignee = godhouse::app()->ConsigneeModel->findByPk($order['consigneeId']);
		$ShoppingcartList = godhouse::app()->ShoppingcartModel->findAll("orderId='{$order['orderId']}'");
		$productIdList = array();
		foreach($ShoppingcartList as $v) {
			$productIdList[$v['productId']] = $v['cartCount'];
		}
		$reportDate = date('Ymd', $order['createdTime']);
		
		foreach ($productIdList as $productId=>$cartCount) {
			$product = godhouse::app()->ProductModel->findByPk($productId);
			$upperparentId = $parentId = $categoryId = 0;
			if($product) {
				$upperparentId = $product['upperparentId'];
				$parentId = $product['parentId'];
				$categoryId = $product['categoryId'];
			}
			$condition = "userId='{$order['userId']}' AND upperparentId='$upperparentId' AND parentId='$parentId' AND categoryId='$categoryId' AND productId='$productId' AND province='{$consignee['province']}' AND city='{$consignee['city']}' AND reportDate='$reportDate'";
			$reportBasis = $this->find($condition);
			if($reportBasis) {
				$this->update($condition, array(
					'cartCount'=>$reportBasis['cartCount'] + $cartCount,
					'totalPrice'=>$reportBasis['price'] + $order['totalPrice'],
				));
			} else {
				$this->insert(array(
					'upperparentId'=>$upperparentId,
					'parentId'=>$parentId,
					'categoryId'=>$categoryId,
					'productId'=>$productId,
					'orderId'=>$order['orderId'],
					'price'=>$order['totalPrice'],
					'userId'=>$order['userId'],
					'province'=>$consignee['province'],
					'city'=>$consignee['city'],
					'cartCount'=>$cartCount,
					'reportDate'=>$reportDate,
					'createdTime'=>time(),
				));
			}
		}
	}
}
?>